INSERT INTO HZBB_KMSUMTMP(ITEMCODE) VALUES (2869);

INSERT INTO GT_KM_IMP(ITEMCODE) SELECT A.ITEM_CODE FROM ACC_SUBJECT_EXT A , HZBB_KMSUMTMP B WHERE  ACC_BOOK=0 AND FINA_ORG='9999' AND FISC_YEA='2024'
 AND A.ITEM_CODE LIKE CONCAT(B.ITEMCODE,'%') GROUP BY A.ITEM_CODE;

INSERT INTO GT_KMDESTTAB
				  (IID, FLGID, ITEMCODE, CODE, CAPTION, LEAF, ITEMLEVEL, STOPED)
						   SELECT SOURCE_IID, OBJ_IID, OBJ_ITEMCODE, OBJ_CODE, OBJ_CAPTION, OBJ_LEAF,
					         OBJ_ITEMLEVEL, OBJ_STOPED
						    FROM (
							  SELECT DISTINCT CONNECT_BY_ROOT(ACC_SUB) AS SOURCE_IID, ACC_SUB AS OBJ_IID,
							                  ITEM_CODE AS OBJ_ITEMCODE, ACCO_CODE AS OBJ_CODE,
							                  SUB_FNAME AS OBJ_CAPTION, IS_BOTT_LAY AS OBJ_LEAF, ACCO_LEVEL OBJ_ITEMLEVEL,
							                  NVL(IS_STOPED, 0) OBJ_STOPED
							    FROM ACC_SUBJECT_EXT 
							  CONNECT BY ITEM_CODE = PRIOR SUPE_ITEM_CODE
							         AND FISC_YEA = PRIOR FISC_YEA
							         AND FINA_ORG = PRIOR FINA_ORG
							         AND ACC_BOOK = PRIOR ACC_BOOK
							   START WITH IS_BOTT_LAY > 0
						          AND FISC_YEA = 2024
						          AND ACC_BOOK = 0
						           AND ITEM_CODE IN ( SELECT ITEMCODE FROM GT_KM_IMP ) 
						          AND FINA_ORG = '9999'
						          )
						           WHERE OBJ_ITEMCODE IN ( SELECT ITEMCODE FROM GT_KM_IMP ) ;
						           
 INSERT INTO GT_ZW_WDBILLMX2
				          (KMID)
				          SELECT A1.IID
				            FROM (SELECT DISTINCT IID, CONNECT_BY_ISLEAF AS LEAF
				                     FROM (SELECT DISTINCT A.ACC_SUB IID, A.ITEM_CODE, A.SUPE_ITEM_CODE
				                              FROM ACC_SUBJECT_EXT A
				                             WHERE FISC_YEA = 2024
				                               AND ACC_BOOK = 0
				                                AND ITEM_CODE IN ( SELECT ITEMCODE FROM GT_KM_IMP ) 
				                               AND FINA_ORG = '9999'
				                               )
				                  			    START WITH ITEM_CODE IN ( SELECT ITEMCODE FROM GT_KM_IMP  ) 
				                   CONNECT BY PRIOR ITEM_CODE = SUPE_ITEM_CODE) A1
				           WHERE A1.LEAF = 1;
				          
	  INSERT INTO GT_ZW_WDBILLMX (KMCAPTION,ZDNAME,COMPID,KMID,QCYE_BWB, SUM_J, SUM_D,
    		  QMYE_BWB,SUM_JL,SUM_DL,ACCOUNT_ID,BZXZ )
    		  
 		 SELECT A1.DIM_GROUP, A1.ZDNAME, A1.COMP_ID, A1.SUBJECT_ID, A1.QCYE_BWB, A1.SUM_J_BWB,
	       A1.SUM_D_BWB, A1.QMYE_BWB, A1.SUM_JL_BWB, A1.SUM_DL_BWB, A1.ACC_PD_ID,0 BZXZ 
			FROM (SELECT  A.DIM_GROUP,NULL ZDNAME, A.FINA_ORG COMP_ID, A.ACC_SUB SUBJECT_ID,
		       SUM(A.CR_BALP_S - A.DR_BALP_S) QCYE_BWB, SUM(A.DR_AMT_TOT) SUM_J_BWB, SUM(A.CR_AMT_TOT) SUM_D_BWB,
		       SUM(A.CR_BALP_E - A.DR_BALP_E) QMYE_BWB, SUM(A.CR_AMT_Y) SUM_JL_BWB,
		       SUM(A.DR_AMT_Y) SUM_DL_BWB, A.ACC_PERIOD ACC_PD_ID 
			  FROM  GT_ZW_WDBILLMX2 Y ,ACC_BAL_DIM_SUM2024_TOP A 
			 WHERE  A.ACC_PERIOD = 20240100
			    AND A.ACC_SUB = Y.KMID 
			    AND A.FINA_ORG = '9999'
			    AND  A.STA_CUR = 1
			   AND A.ACC_BOOK = 0
			    AND A.ENTRY_MARK = 1 
			 GROUP BY  A.FINA_ORG , A.ACC_SUB, A.ACC_PERIOD,A.DIM_GROUP  ) A1
			  WHERE   ABS(A1.SUM_J_BWB) + ABS(A1.SUM_D_BWB) + ABS(A1.QCYE_BWB) +  ABS(A1.QMYE_BWB) >0 
			  AND ROWNUM<=1000;
			  
			 DELETE FROM GT_ZW_WDBILLMX2;
			 
			 INSERT INTO GT_ZW_WDBILLMX2(KMCAPTION, DIM024, DIM003, DIM_CA_FLOW_CLAS, DIM010, DIM_BANK_ACC)
				SELECT A.DIM_GROUP,MAX(DECODE(A.DIM_TYPE_ID,'1041',A.DIM_VAL,NULL)) DIM024,
				MAX(DECODE(A.DIM_TYPE_ID,'1249',NVL(A.DIM_VAL,A.DIM_VAL),NULL)) DIM003,
				MAX(DECODE(A.DIM_TYPE_ID,'5181',A.DIM_VAL,NULL)) DIM_CA_FLOW_CLAS,
				MAX(DECODE(A.DIM_TYPE_ID,'8909',NVL(A.DIM_VAL,A.DIM_VAL),NULL)) DIM010,MAX(DECODE(A.DIM_TYPE_ID,'5029',NVL(A.DIM_VAL,A.DIM_VAL),NULL)) DIM_BANK_ACC
				FROM ACC_DIM_GROUP A,(SELECT DISTINCT KMCAPTION FROM GT_ZW_WDBILLMX) B WHERE A.DIM_GROUP = B.KMCAPTION
				GROUP BY A.DIM_GROUP;
				
			
			 INSERT INTO GT_ZW_WDBILLMX
			  (ZDNAME, COMPID, KMID, QCYE_BWB, SUM_J, SUM_D, QMYE_BWB, SUM_JL, SUM_DL,
			   ACCOUNT_ID, BZXZ, DIM024, DIM003, DIM_CA_FLOW_CLAS, DIM010, DIM_BANK_ACC)
			  SELECT A.ZDNAME, A.COMPID, A.KMID, A.QCYE_BWB, A.SUM_J, A.SUM_D,
			         A.QMYE_BWB, A.SUM_JL, A.SUM_DL, A.ACCOUNT_ID, 1 BZXZ, B.DIM024, B.DIM003, B.DIM_CA_FLOW_CLAS, B.DIM010, B.DIM_BANK_ACC
			    FROM GT_ZW_WDBILLMX A, GT_ZW_WDBILLMX2 B
			   WHERE A.KMCAPTION = B.KMCAPTION;
			   
			   DELETE FROM  GT_ZW_WDBILLMX WHERE BZXZ = 0;
			   
			   UPDATE GT_ZW_WDBILLMX SET TYPEBZ = 1 WHERE ACCOUNT_ID = 20240100 ; 
			   
			  
			  SELECT ZDNAME, COMPID, KMID, QCYE_BWB, SUM_J, SUM_D, QMYE_BWB, SUM_JL, SUM_DL,
			   ACCOUNT_ID, BZXZ, DIM024, DIM003, DIM_CA_FLOW_CLAS, DIM010, DIM_BANK_ACC,TYPEBZ FROM  GT_ZW_WDBILLMX WHERE TYPEBZ = 1 AND QCYE_BWB = -94408.8
			  
			  			  SELECT count(*) FROM  GT_ZW_WDBILLMX WHERE TYPEBZ = 1;
			  			 	  SELECT count(*) FROM  GT_ZW_WDBILLMX WHERE TYPEBZ IS null;
			  
			  		  SELECT ZDNAME, COMPID, KMID, QCYE_BWB, SUM_J, SUM_D, QMYE_BWB, SUM_JL, SUM_DL,
			   ACCOUNT_ID, BZXZ, DIM024, DIM003, DIM_CA_FLOW_CLAS, DIM010, DIM_BANK_ACC,TYPEBZ FROM  GT_ZW_WDBILLMX WHERE TYPEBZ IS NULL AND QCYE_BWB = -94408.8
			   
			  130690	88	0	0	88	0	0	20240100	1	14700997	1256498	14700588		1379374
			    INSERT INTO GT_ZW_WDBILLMX (KMCAPTION,ZDNAME,COMPID,KMID,QCYE_BWB, SUM_J, SUM_D,
    		  QMYE_BWB,SUM_JL,SUM_DL,ACCOUNT_ID,BZXZ )
    		  
 		 SELECT A1.DIM_GROUP, A1.ZDNAME, A1.COMP_ID, A1.SUBJECT_ID, A1.QCYE_BWB, A1.SUM_J_BWB,
	       A1.SUM_D_BWB, A1.QMYE_BWB, A1.SUM_JL_BWB, A1.SUM_DL_BWB, A1.ACC_PD_ID,0 BZXZ 
			FROM (SELECT  A.DIM_GROUP,NULL ZDNAME, A.FINA_ORG COMP_ID, A.ACC_SUB SUBJECT_ID,
		       SUM(A.CR_BALP_S - A.DR_BALP_S) QCYE_BWB, SUM(A.DR_AMT_TOT) SUM_J_BWB, SUM(A.CR_AMT_TOT) SUM_D_BWB,
		       SUM(A.CR_BALP_E - A.DR_BALP_E) QMYE_BWB, SUM(A.CR_AMT_Y) SUM_JL_BWB,
		       SUM(A.DR_AMT_Y) SUM_DL_BWB, A.ACC_PERIOD ACC_PD_ID 
			  FROM  GT_ZW_WDBILLMX2 Y ,ACC_BAL_DIM_SUM2024_TOP A 
			 WHERE  A.ACC_PERIOD = 20240100
			    AND A.ACC_SUB = Y.KMID 
			    AND A.FINA_ORG = '9999'
			    AND  A.STA_CUR = 1
			   AND A.ACC_BOOK = 0
			    AND A.ENTRY_MARK = 1 
			 GROUP BY  A.FINA_ORG , A.ACC_SUB, A.ACC_PERIOD,A.DIM_GROUP  ) A1
			  WHERE   ABS(A1.SUM_J_BWB) + ABS(A1.SUM_D_BWB) + ABS(A1.QCYE_BWB) +  ABS(A1.QMYE_BWB) >0 
			  AND ROWNUM<=1000;
			  
			 
			    INSERT INTO GT_ZW_WDBILLMX (ZDNAME,COMPID,KMID,QCYE,QCYE_BWB,WD56,WD57,SUM_WB_J,SUM_J, SUM_WB_D,SUM_D,
                              QMYE,QMYE_BWB,SUM_WB_JL,SUM_JL,SUM_WB_DL,SUM_DL,ACCOUNT_ID,MONEY2,TYPEBZ , DIM024, DIM003, DIM_CA_FLOW_CLAS, DIM010, DIM_BANK_ACC)
                  SELECT
                    A.ZDNAME,
                    A.COMPID,
                    A.KMID,
                    A.QCYE,
                    A.QCYE_BWB,
                    B.QCYE AS QCYE_NC,
                    B.QCYE_BWB AS QCYE_BWB_NC,
                    A.SUM_WB_J,
                    A.SUM_J,
                    A.SUM_WB_D,
                    A.SUM_D,
                    A.QMYE,
                    A.QMYE_BWB,
                    A.SUM_WB_JL,
                    A.SUM_JL,
                    A.SUM_WB_DL,
                    A.SUM_DL,
                    A.ACCOUNT_ID,
                    A.MONEY2,
                    2 TYPEBZ
                    ,A.DIM024,A.DIM003,A.DIM_CA_FLOW_CLAS,A.DIM010,A.DIM_BANK_ACC
                FROM
                    GT_ZW_WDBILLMX A
                LEFT JOIN
                    GT_ZW_WDBILLMX B
                ON
                    A.COMPID = B.COMPID
                    AND A.KMID = B.KMID
                     AND A.DIM024 = B.DIM024  AND A.DIM_CA_FLOW_CLAS = B.DIM_CA_FLOW_CLAS AND A.DIM010 = B.DIM010 AND A.DIM_BANK_ACC = B.DIM_BANK_ACC
                    AND B.TYPEBZ = 1
                    AND B.QCYE_BWB = -94408.8
                WHERE
                    A.QCYE_BWB = -94408.8
                    AND A.TYPEBZ IS NULL;
                    
                   
                                     SELECT
                    A.ZDNAME,
                    A.COMPID,
                    A.KMID,
                    A.QCYE,
                    A.QCYE_BWB,
                    B.QCYE AS QCYE_NC,
                    B.QCYE_BWB AS QCYE_BWB_NC,
                    A.SUM_WB_J,
                    A.SUM_J,
                    A.SUM_WB_D,
                    A.SUM_D,
                    A.QMYE,
                    A.QMYE_BWB,
                    A.SUM_WB_JL,
                    A.SUM_JL,
                    A.SUM_WB_DL,
                    A.SUM_DL,
                    A.ACCOUNT_ID,
                    A.MONEY2,
                    2 TYPEBZ
                    ,A.DIM024,A.DIM003,A.DIM_CA_FLOW_CLAS,A.DIM010,A.DIM_BANK_ACC
                FROM
                    GT_ZW_WDBILLMX A
                LEFT JOIN
                    GT_ZW_WDBILLMX B
                ON
                    A.COMPID = B.COMPID
                    AND A.KMID = B.KMID
--                     AND COALESCE(A.DIM024,0) =  COALESCE(B.DIM024,0)  
--                     AND  COALESCE(A.DIM_CA_FLOW_CLAS,0) =  COALESCE(B.DIM_CA_FLOW_CLAS,0) 
--                     AND  COALESCE(A.DIM010,0) =  COALESCE(B.DIM010,0) 
--                     AND  COALESCE(A.DIM_BANK_ACC,0) =  COALESCE(B.DIM_BANK_ACC,0)
                     AND COALESCE(A.DIM024 ,0) = COALESCE(B.DIM024,0) 
                     AND COALESCE(A.DIM_CA_FLOW_CLAS ,0) = COALESCE(B.DIM_CA_FLOW_CLAS,0)
                     AND COALESCE(A.DIM010 ,0) = COALESCE(B.DIM010,0) 
                     AND COALESCE(A.DIM_BANK_ACC ,0) = COALESCE(B.DIM_BANK_ACC,0)
                    AND B.TYPEBZ = 1
                WHERE
                     A.TYPEBZ IS NULL;